package com.weitu.contractor.service.impl;

import com.weitu.contractor.entity.ManagerAudit;
import com.weitu.contractor.entity.ManagerAuditCriteria;
import com.weitu.contractor.mapper.ManagerAuditMapper;
import com.weitu.contractor.service.ManagerAuditService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ManagerAuditServiceImpl implements ManagerAuditService {
    private final ManagerAuditMapper managerAuditMapper;

    private static final Logger logger = LoggerFactory.getLogger(ManagerAuditServiceImpl.class);

    @Autowired
    public ManagerAuditServiceImpl(ManagerAuditMapper managerAuditMapper) {
        this.managerAuditMapper = managerAuditMapper;
    }

    @Override
    public int countByCriteria(ManagerAuditCriteria example) {
        int count = this.managerAuditMapper.countByCriteria(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Override
    public ManagerAudit selectByPrimaryKey(String id) {
        return this.managerAuditMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ManagerAudit> selectByCriteria(ManagerAuditCriteria example) {
        return this.managerAuditMapper.selectByCriteria(example);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(String id) {
        return this.managerAuditMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(ManagerAudit record) {
        return this.managerAuditMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int updateByCriteriaSelective(ManagerAudit record, ManagerAuditCriteria example) {
        return this.managerAuditMapper.updateByCriteriaSelective(record, example);
    }

    @Override
    @Transactional
    public int insertSelective(ManagerAudit record) {
        return this.managerAuditMapper.insertSelective(record);
    }

    @Override
    public boolean checkIfExist(String contractorAuditId, String managerId, String projectId) {
        ManagerAuditCriteria criteria = new ManagerAuditCriteria();
        criteria.createCriteria()
                .andDelStateEqualTo(0)
                .andContractorAuditIdEqualTo(contractorAuditId)
                .andManagerIdEqualTo(managerId)
                .andProjectIdEqualTo(projectId);
        return this.managerAuditMapper.countByCriteria(criteria) > 0;
    }

    @Override
    public boolean checkIfExist(String contractorAuditId, String managerId, String projectId, String id) {
        ManagerAuditCriteria criteria = new ManagerAuditCriteria();
        criteria.createCriteria()
                .andDelStateEqualTo(0)
                .andContractorAuditIdEqualTo(contractorAuditId)
                .andManagerIdEqualTo(managerId)
                .andProjectIdEqualTo(projectId)
                .andIdNotEqualTo(id);
        return this.managerAuditMapper.countByCriteria(criteria) > 0;
    }
}